package blackberry.util.hotkey;

import java.util.Vector;

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.CodeModuleManager;

public class DjHotkeyRecover {

	private char newestHotkey;
	private char discardedHotkey;
	
	//Rim에서 만든 앱은 description에 일반사용자가 만든것은 name에 언더바가 존재함
	public void recoverHotkeyWithDescriptor(ApplicationDescriptor recoveryApp){
		if (DjHotkeyManager.isRimApp(recoveryApp)) {
			 ;
			DjHotkey.setIconName(
					CodeModuleManager.getModuleDescription(recoveryApp.getModuleHandle()).toString(),
					recoveryApp);
		} else {
			DjHotkey.setIconName(recoveryApp.getName(), recoveryApp);
		}
	}
	
	public void recoverDiscardedHotkey(char newHotkey){
		updateNewestKeyAndDiscardedKey(newHotkey);
		if (isVoidHotkey(discardedHotkey)) 
			return;
		Vector discardedHotkeyMatchList = DjHotkeyManager.getDescriptorsOf(discardedHotkey);
		for (int i = 0; i < discardedHotkeyMatchList.size(); i++){
			recoverHotkeyWithDescriptor((ApplicationDescriptor) discardedHotkeyMatchList
					.elementAt(i));
		}
	}

	private void updateNewestKeyAndDiscardedKey(char newHotkey){
		this.discardedHotkey = this.newestHotkey;
		this.newestHotkey = newHotkey;
	}
	
	char getNewestHotkey() {
		return newestHotkey;
	}

	char getDiscardedHotkey() {
		return discardedHotkey;
	}
	
	static private boolean isVoidHotkey(char newHotkey) {
		return newHotkey == 0;
	}
}
